package cn.edu.xust.service.impl;

import cn.edu.xust.dao.SysUserhotelDao;
import cn.edu.xust.entity.HotelEntity;
import cn.edu.xust.entity.SysUserEntity;
import cn.edu.xust.entity.SysUserhotelEntity;
import cn.edu.xust.Ro.HotelPCRo;
import cn.edu.xust.Ro.chotelRo;
import cn.edu.xust.service.HotelService;
import cn.edu.xust.service.SysUserhotelService;
import cn.edu.xust.util.PageUtils;
import cn.edu.xust.util.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("sysUserhotelService")
public class SysUserhotelServiceImpl extends ServiceImpl<SysUserhotelDao, SysUserhotelEntity> implements SysUserhotelService {
    public static final String MANAGER = "-1";
    public static final String GROUPS = "0";
    public static final String GROUP = "1";
    public static final String PERSON = "2";
    @Autowired
    HotelService hotelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysUserhotelEntity> page = this.page(
                new Query<SysUserhotelEntity>().getPage(params),
                new QueryWrapper<SysUserhotelEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SysUserhotelEntity> getByUid(Integer uid) {


        QueryWrapper<SysUserhotelEntity> wap = new QueryWrapper<SysUserhotelEntity>().in("uid", uid);
        return baseMapper.selectList(wap);
    }

    @Override
    public List<chotelRo> getHotelListByNP(SysUserEntity userEntity) {


        String type = userEntity.getUtype();
        List<chotelRo> chotelRos = new ArrayList<>();

        //管理员直接获取所有酒店
        if (MANAGER.equals(type)) {
            List<HotelEntity> allHotel = hotelService.getAllHotel();
            for (HotelEntity hotelEntity : allHotel) {
                chotelRo chotelRo = new chotelRo();
                chotelRo.setHotelCname(hotelEntity.getHotelCname());
                chotelRo.setHotelId(hotelEntity.getHotelId());
                chotelRos.add(chotelRo);
            }
        }
        if (GROUPS.equals(type)) {
            List<HotelEntity> allHotel = hotelService.getByGroupId(String.valueOf(userEntity.getGid()));
            allHotel = allHotel.stream().filter(e -> userEntity.getGid().equals(e.getHotelGroup())).collect(Collectors.toList());
            for (HotelEntity hotelEntity : allHotel) {
                chotelRo chotelRo = new chotelRo();
                chotelRo.setHotelCname(hotelEntity.getHotelCname());
                chotelRo.setHotelId(hotelEntity.getHotelId());
                chotelRos.add(chotelRo);
            }
        }
        if (GROUP.equals(type)) {
            List<HotelEntity> allHotel = hotelService.getByGroupId(String.valueOf(userEntity.getGid()));
            allHotel = allHotel.stream().filter(e -> userEntity.getGid().equals(e.getHotelGroup())).collect(Collectors.toList());
            for (HotelEntity hotelEntity : allHotel) {
                chotelRo chotelRo = new chotelRo();
                chotelRo.setHotelCname(hotelEntity.getHotelCname());
                chotelRo.setHotelId(hotelEntity.getHotelId());
                chotelRos.add(chotelRo);
            }
        }
        if (PERSON.equals(type)) {
            //查询userhotel表
            List<SysUserhotelEntity> sysUserhotelEntities = this.getByUid(userEntity.getUid());
            //拿到酒店号
            List<Integer> hotelids = sysUserhotelEntities.stream().map(SysUserhotelEntity::getHotelid).collect(Collectors.toList());
            //获取酒店具体中文信息
            List<HotelEntity> allHotel = hotelService.getByHotelidList(hotelids);
            //筛选用户gid=酒店gid的
            allHotel = allHotel.stream().filter(e -> userEntity.getGid().equals(e.getHotelGroup())).collect(Collectors.toList());
            for (HotelEntity hotelEntity : allHotel) {
                chotelRo chotelRo = new chotelRo();
                chotelRo.setHotelCname(hotelEntity.getHotelCname());
                chotelRo.setHotelId(hotelEntity.getHotelId());
                chotelRos.add(chotelRo);
            }
        }
        return chotelRos;
    }

    @Override
    public boolean resetByuid(Integer uid, Integer gid, List<Integer> checked_hotel) {

        //删除
        this.removeByUid(uid);

        //添加
        List<SysUserhotelEntity> sysUserhotelEntities = new ArrayList<>();
        for (Integer integer : checked_hotel) {

            SysUserhotelEntity sysUserhotelEntity = new SysUserhotelEntity();
            sysUserhotelEntity.setUid(uid);
            sysUserhotelEntity.setHotelid(integer);
            sysUserhotelEntity.setGid(gid);
            sysUserhotelEntities.add(sysUserhotelEntity);
        }
        return this.saveBatch(sysUserhotelEntities);
    }

    @Override
    public boolean removeByUid(Integer uid) {
        QueryWrapper<SysUserhotelEntity> wap = new QueryWrapper<SysUserhotelEntity>().eq("uid", uid);
        return baseMapper.delete(wap) > 0;
    }

    @Override
    public List<HotelPCRo> getHotelListByNP2(SysUserEntity userEntity) {

        String type = userEntity.getUtype();
        List<HotelPCRo> chotelRos = new ArrayList<>();

        //管理员直接获取所有酒店
        if (MANAGER.equals(type)) {
            List<HotelEntity> allHotel = hotelService.getAllHotel();
            for (HotelEntity hotelEntity : allHotel) {
                HotelPCRo chotelRo = new HotelPCRo();
                chotelRo.setHotelCname(hotelEntity.getHotelCname());
                chotelRo.setHotelId(hotelEntity.getHotelId());
                chotelRo.setPid(hotelEntity.getProvinceid());
                chotelRo.setCid(hotelEntity.getCityid());
                chotelRos.add(chotelRo);
            }
        }
        if (GROUPS.equals(type)) {
            List<HotelEntity> allHotel = hotelService.getByGroupId(String.valueOf(userEntity.getGid()));
            allHotel = allHotel.stream().filter(e -> userEntity.getGid().equals(e.getHotelGroup())).collect(Collectors.toList());
            for (HotelEntity hotelEntity : allHotel) {
                HotelPCRo chotelRo = new HotelPCRo();
                chotelRo.setHotelCname(hotelEntity.getHotelCname());
                chotelRo.setHotelId(hotelEntity.getHotelId());         chotelRo.setPid(hotelEntity.getProvinceid());
                chotelRo.setCid(hotelEntity.getCityid());
                chotelRos.add(chotelRo);
            }
        }
        if (GROUP.equals(type)) {
            List<HotelEntity> allHotel = hotelService.getByGroupId(String.valueOf(userEntity.getGid()));
            allHotel = allHotel.stream().filter(e -> userEntity.getGid().equals(e.getHotelGroup())).collect(Collectors.toList());
            for (HotelEntity hotelEntity : allHotel) {
                HotelPCRo chotelRo = new HotelPCRo();
                chotelRo.setHotelCname(hotelEntity.getHotelCname());
                chotelRo.setHotelId(hotelEntity.getHotelId());
                chotelRo.setPid(hotelEntity.getProvinceid());
                chotelRo.setCid(hotelEntity.getCityid());
                chotelRos.add(chotelRo);
            }
        }
        if (PERSON.equals(type)) {
            //查询userhotel表
            List<SysUserhotelEntity> sysUserhotelEntities = this.getByUid(userEntity.getUid());
            //拿到酒店号
            List<Integer> hotelids = sysUserhotelEntities.stream().map(SysUserhotelEntity::getHotelid).collect(Collectors.toList());
            //获取酒店具体中文信息
            List<HotelEntity> allHotel = hotelService.getByHotelidList(hotelids);
            //筛选用户gid=酒店gid的
            allHotel = allHotel.stream().filter(e -> userEntity.getGid().equals(e.getHotelGroup())).collect(Collectors.toList());
            for (HotelEntity hotelEntity : allHotel) {
                HotelPCRo chotelRo = new HotelPCRo();
                chotelRo.setHotelCname(hotelEntity.getHotelCname());
                chotelRo.setHotelId(hotelEntity.getHotelId());
                chotelRo.setPid(hotelEntity.getProvinceid());
                chotelRo.setCid(hotelEntity.getCityid());
                chotelRos.add(chotelRo);
            }
        }
        return chotelRos;


    }

    @Override
    public List<SysUserhotelEntity> getByUids(List<Integer> bindsysids) {


        QueryWrapper<SysUserhotelEntity> wap = new QueryWrapper<SysUserhotelEntity>().in("uid", bindsysids);
        return baseMapper.selectList(wap);



    }
}

